package com.fdb.efp.cus.service.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

import com.fdb.efp.cus.service.dao.PartnerInfoDao;
import com.fdb.efp.cus.service.domain.PartnerInfo;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.cus.service.facade.PartnerInfoService;
import com.fdb.efp.cus.service.vo.PartnerInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>ServiceImpl: 合作方信息表				<p>
 * @author ligm
 * @date 2019-09-04
 */
@Service("partnerInfoService")
public class PartnerInfoServiceImpl implements PartnerInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(PartnerInfoServiceImpl.class);

	@Autowired
	private PartnerInfoDao partnerInfoDao;
	
	/**
	 * 新增操作
	 */
	public int insertPartnerInfo(PartnerInfoVO inPartnerInfoVo) {
		logger.debug("当前新增数据为:"+ inPartnerInfoVo.toString());
		int num = 0;
		try {
			PartnerInfo partnerInfo = new PartnerInfo();
			beanCopy(inPartnerInfoVo, partnerInfo);
			num = partnerInfoDao.insertPartnerInfo(partnerInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(PartnerInfoVO inPartnerInfoVo) {
		logger.debug("当前删除数据条件为:"+ inPartnerInfoVo);
		int num = 0;
		try {
			PartnerInfo partnerInfo = new PartnerInfo();
			beanCopy(inPartnerInfoVo, partnerInfo);
			num = partnerInfoDao.deleteByPk(partnerInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPartnerInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(PartnerInfoVO inPartnerInfoVo) {
		logger.debug("当前修改数据为:"+ inPartnerInfoVo.toString());
		int num = 0;
		try {
			PartnerInfo partnerInfo = new PartnerInfo();
			beanCopy(inPartnerInfoVo, partnerInfo);
			num = partnerInfoDao.updateByPk(partnerInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPartnerInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public PartnerInfoVO queryByPk(PartnerInfoVO inPartnerInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inPartnerInfoVo);
		try {
			PartnerInfo queryPartnerInfo = new PartnerInfo();
			beanCopy(inPartnerInfoVo, queryPartnerInfo);
			PartnerInfo queryRslPartnerInfo = partnerInfoDao.queryByPk(queryPartnerInfo);
			if (Objects.nonNull(queryRslPartnerInfo)) {
				PartnerInfoVO outPartnerInfoVo = beanCopy(queryRslPartnerInfo, new PartnerInfoVO());
				logger.debug("当前查询结果为:"+ outPartnerInfoVo.toString());
				return outPartnerInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PartnerInfoVO> queryAllOwner(PartnerInfoVO partnerInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PartnerInfoVO> list = null;
		try {
			List<PartnerInfo> partnerInfos = partnerInfoDao.queryAllOwnerByPage(partnerInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ partnerInfos.size());
			pageSet(partnerInfos, partnerInfoVo);
			list = (List<PartnerInfoVO>) beansCopy(partnerInfos, PartnerInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PartnerInfoVO> queryAllCurrOrg(PartnerInfoVO partnerInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<PartnerInfo> partnerInfos = partnerInfoDao.queryAllCurrOrgByPage(partnerInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+partnerInfos.size());
		List<PartnerInfoVO> list = null;
		try {
			pageSet(partnerInfos, partnerInfoVo);
			list = (List<PartnerInfoVO>) beansCopy(partnerInfos, PartnerInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PartnerInfoVO> queryAllCurrDownOrg(PartnerInfoVO partnerInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<PartnerInfo> partnerInfos = partnerInfoDao.queryAllCurrDownOrgByPage(partnerInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ partnerInfos.size());
		List<PartnerInfoVO> list = null;
		try {
			pageSet(partnerInfos, partnerInfoVo);
			list = (List<PartnerInfoVO>) beansCopy(partnerInfos, PartnerInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@Override
	public PartnerInfoVO queryByIdAndName(PartnerInfoVO partnerInfoVo) {
		logger.debug("当前查询参数信息为:"+ partnerInfoVo);
		try {
			PartnerInfo queryPartnerInfo = new PartnerInfo();
			beanCopy(partnerInfoVo, queryPartnerInfo);
			PartnerInfo queryRslPartnerInfo = partnerInfoDao.queryByIdAndName(queryPartnerInfo);
			if (Objects.nonNull(queryRslPartnerInfo)) {
				PartnerInfoVO outPartnerInfoVo = beanCopy(queryRslPartnerInfo, new PartnerInfoVO());
				logger.debug("当前查询结果为:"+ outPartnerInfoVo.toString());
				return outPartnerInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public PartnerInfoVO queryBySimName(PartnerInfoVO partnerInfoVo) {
		logger.debug("当前查询参数信息为:"+ partnerInfoVo);
		try {
			PartnerInfo queryPartnerInfo = new PartnerInfo();
			beanCopy(partnerInfoVo, queryPartnerInfo);
			PartnerInfo queryRslPartnerInfo = partnerInfoDao.queryBySimName(queryPartnerInfo);
			if (Objects.nonNull(queryRslPartnerInfo)) {
				PartnerInfoVO outPartnerInfoVo = beanCopy(queryRslPartnerInfo, new PartnerInfoVO());
				logger.debug("当前查询结果为:"+ outPartnerInfoVo.toString());
				return outPartnerInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public PartnerInfoVO queryByName(PartnerInfoVO partnerInfoVo) {
		logger.debug("当前查询参数信息为:"+ partnerInfoVo);
		try {
			PartnerInfo queryPartnerInfo = new PartnerInfo();
			beanCopy(partnerInfoVo, queryPartnerInfo);
			PartnerInfo queryRslPartnerInfo = partnerInfoDao.queryByName(queryPartnerInfo);
			if (Objects.nonNull(queryRslPartnerInfo)) {
				PartnerInfoVO outPartnerInfoVo = beanCopy(queryRslPartnerInfo, new PartnerInfoVO());
				logger.debug("当前查询结果为:"+ outPartnerInfoVo.toString());
				return outPartnerInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public int inValidPartnerOverCooprEndDt() {
		LocalDate date = LocalDate.now();
		logger.info("======>终止合作日期到期的合作方信息开始，当前日期:<======"+date);
		int num = 0;
		try {
			num = partnerInfoDao.inValidPartnerOverCooprEndDt();
			logger.info("======>终止合作日期到期的合作方信息结束，终止的合作方条数为:<======"+num);
		} catch (Exception e) {
			num = -1;
			logger.error("终止合作日期到期的合作方信息发生异常，异常信息为:"+e.getMessage());
		}
		return num;
	}

}
